<template>
    <div id="giftActivity" style="width:90%;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px">
            <div style="text-align:right">
              <el-button @click="goback()" size="mini" type="primary" style="margin-right:10px">返回</el-button>
            </div>

            <el-form-item label="活动名称：" prop="activityName">
              <el-input
                    type="textarea"
                    v-model="ruleForm.activityName"
                    :disabled="disabledFlag"
                    placeholder="请输入活动名称"
                    maxlength="10"
                    show-word-limit
                    style="width:250px;"
                ></el-input>
            </el-form-item>
            <el-form-item label="活动时间：" prop="runTimeRange">
              <el-date-picker
                v-model="ruleForm.runTimeRange"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions"
                :clearable="false"
                :disabled="disabledFlag"
                @change="changeValidDate"
              ></el-date-picker>
            </el-form-item>
            

            <el-form-item label="活动商品：" prop="goodsCount">
              <el-button type="primary" @click="showGoodsDialog()" icon="el-icon-plus" size="mini" style="margin:5px 0 20px 0"
                v-if="!disabledFlag">添加商品</el-button>
                <el-button type="primary" @click="showGoodsDialog2()" size="mini" style="margin:5px 0 20px 0"
                v-if="!disabledFlag" :disabled="batchSelGifts.length===0">对选中商品设置</el-button>
              <span style="color:red">{{isGiftEmpty}}</span>
              <!--商品列表-->
              <div id="giftActivityGoodsTable">
                <el-table
                    ref="goodsTable"
                    :data="goodsList"
                    border
                    style="width:100%;"
                    v-loading="loading"
                    @selection-change="handleSelectionChange3"
                    :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
                >   
                    <el-table-column type="selection" width="60" align="center" fixed disabled></el-table-column>
                    <el-table-column
                        prop="goodsSeq"
                        column-key="goodsSeq"
                        label="商品编码"
                        align="center"
                    ></el-table-column>
                    <el-table-column
                        prop="goodsName"
                        column-key="goodsName"
                        label="商品名称"
                        align="center"
                    ></el-table-column>
                    <el-table-column 
                       prop="unitName" 
                       label="计量单位" 
                       align="center"
                    ></el-table-column>
                    <el-table-column prop="bonuspointMultiple" width="160px"  label="积分倍数" align="center">
                        <template slot-scope="scope">
                            <el-input
                                type="number"
                                :min="1"
                                oninput="value=value.replace(/[^0-9]/g,'')"
                                v-model="scope.row.bonuspointMultiple"
                                style="width:120px;"
                                v-on:blur="buyCountCheck(scope.row,scope.$index)"
                                :disabled="disabledFlag"
                            >
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column  label="有效期" width="460px" align="center">
                        <template slot-scope="scope">
                            <el-date-picker
                                    v-model="scope.row.validDateRange"
                                    type="daterange"
                                    align="right"
                                    unlink-panels
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    value-format="yyyy-MM-dd"
                                    :picker-options="pickerLimitOptions"
                                    :clearable="false"
                                    :disabled="disabledFlag"
                                  ></el-date-picker>
                                </template>
                    </el-table-column>
                    <el-table-column label="活动说明：" width="160px" align="center">
                        <template slot-scope="scope">    
                          <el-button size="mini" @click="showGiftsDialog(scope.row,scope.$index)" type="text">修改</el-button>          
                        </template>
                    </el-table-column>
                    
                    <el-table-column label="操作" align="center" v-if="!disabledFlag">
                      <template slot-scope="scope">
                        <el-button size="mini" @click="deleteGoodsItem(scope.row,scope.$index)" type="text">删除</el-button>
                      </template>
                    </el-table-column>
                </el-table>
              </div>
            </el-form-item>
            <el-form-item label="活动描述：" prop="remark">
                <el-input
                    type="textarea"
                    v-model="ruleForm.remark"
                    :disabled="disabledFlag"
                    placeholder="请输入活动描述"
                    maxlength="120"
                    show-word-limit
                    :autosize="{ minRows: 4 }"
                    style="width:650px;"
                ></el-input>
            </el-form-item>
            <el-form-item label="发布状态：">
                <el-radio-group v-model="ruleForm.status" :disabled="disabledFlag">
                    <el-radio-button label="0">不发布</el-radio-button>
                    <el-radio-button label="1">发布</el-radio-button>
                </el-radio-group>
            </el-form-item>

            <el-form-item style="width:100%;text-align:center">
                <el-button @click="resetForm('ruleForm')" v-show="!this.disabledFlag">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')" v-show="!this.disabledFlag" :loading="submitLoading">保 存</el-button>
            </el-form-item>
        </el-form>

        <!--添加活动商品弹窗-->
        <el-dialog title="添加活动商品" :visible.sync="insertGoodsDialogVisible" width="60%">
          <div>
            <el-form :inline="true" :model="formInline2" class="demo-form-inline">
              <el-row :span="24">
                <el-col :span="24">
                  <el-form-item label="商品编码：">
                    <el-input v-model="formInline2.goodsSeq" clearable placeholder style="width:200px"></el-input>
                  </el-form-item>
                  <el-form-item label="商品名称：">
                    <el-input v-model="formInline2.goodsName" clearable placeholder style="width:200px"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="search2()" size="mini" icon="el-icon-search">查询</el-button>
                  <!-- </el-form-item>
                  <el-form-item> -->
                    <el-button type="primary" @click="kong2()" size="mini" icon="el-icon-delete">清空</el-button>
                    <el-button 
                    type="info"
                    @click="closeGoodsDialog()" 
                    size="mini"
                  >取 消</el-button>
                  <el-button
                    type="primary"
                    @click="insertGoods()"
                    size="mini"
                  >确 认</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div class="table2">
              <el-table ref="table2"
                  tooltip-effect="dark"
                  style="width: 100%"
                  v-loading="loading2"
                  :data="chooseGoodsList"
                  @selection-change="handleSelectionChange2"
                  border
                  :header-cell-style="{background:'#eef1f6',color:'#606266'}"
              >
                <el-table-column type="selection" width="60" align="center" fixed disabled></el-table-column>
                <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
                <el-table-column prop="goodsPic" label="商品图片" align="center" width="150">
                  <template slot-scope="scope">
                      <img
                        :src="scope.row.goodsPic"
                        v-if="scope.row.goodsPic !='' && scope.row.goodsPic !=null"
                        width="70"
                        height="50"
                      />
                  </template>
                </el-table-column>
                <el-table-column prop="goodsSeq" label="商品编码" align="center" width="150" show-overflow-tooltip></el-table-column>
                <el-table-column prop="goodsName" label="商品名称" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="unitName" label="计量单位" align="center" width="100"></el-table-column>
                <el-table-column label="商品描述" align="center" show-overflow-tooltip width="300" >
                  <template slot-scope="scope">
                    <div v-html="scope.row.goodsDescribe"></div>
                  </template>
                </el-table-column>
              </el-table>
              <div class="block" style="margin-top:22px;overflow:hidden">
                <el-pagination
                  @size-change="handleSizeChange2"
                  @current-change="handleCurrentChange2"
                  :page-sizes="[5, 10, 20, 30, 50, 100, 150, 200]"
                  :current-page="listQuery2.current"
                  :page-size="listQuery2.size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total2"
                ></el-pagination>
              </div>
              <!-- <el-row>
                <el-col :span="24" style="text-align: center;margin-top: 30px">
                  <el-button 
                    type="info"
                    @click="closeGoodsDialog()" 
                    size="mini"
                  >取 消</el-button>
                  <el-button
                    type="primary"
                    @click="insertGoods()"
                    size="mini"
                  >确 认</el-button>
                </el-col>
              </el-row> -->
            </div>
          </div>
        </el-dialog>

        <!--添加商品赠品弹窗-->
        <el-dialog title="活动说明" :visible.sync="insertGiftsDialogVisible" width="60%">
          <div>
            <el-form :inline="true" class="demo-form-inline">
                  <el-form-item label="">
                    <el-input type="textarea" :rows="5" v-model="activityDesc" placeholder="请输入活动说明" style="width:500px" :disabled="disabledFlag"></el-input>
                  </el-form-item>
                  <!-- <el-input v-model="formInline3.good" type="hidden"></el-input> -->
            </el-form>
            
              <el-row>
                <el-col :span="24" style="text-align: center;margin-top: 30px">
                  <el-button 
                    type="info"
                    @click="closeGiftsDialog()" 
                    size="mini"
                  >取 消</el-button>
                  <el-button
                    type="primary"
                    @click="insertGifts()"
                    size="mini"
                    v-show="!this.disabledFlag"
                  >确 认</el-button>
                </el-col>
              </el-row>
          </div>
        </el-dialog>

        <!--添加商品赠品弹窗-->
        <el-dialog title="选中商品设置" :visible.sync="insertGoodsDialogVisible2" width="60%">
          <div>
            <el-form :model="formInline4" :rules="rulesGoods"  ref="formInline4" class="demo-form-inline">
                  <el-form-item label="积分倍数：" prop="bonuspointMultiple">
                    <el-input v-model="formInline4.bonuspointMultiple" clearable placeholder style="width:200px"
                     ></el-input>
                  </el-form-item>
                  <el-form-item label="有效期：" prop="validDateRange">
                     <el-date-picker
                                    v-model="formInline4.validDateRange"
                                    type="daterange"
                                    align="right"
                                    style="width:250px"
                                    unlink-panels
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    value-format="yyyy-MM-dd"
                                    :picker-options="pickerLimitOptions"
                                    :clearable="false"
                                    :disabled="disabledFlag"
                                  ></el-date-picker>
                    </el-form-item>
                  <el-form-item label="活动说明" prop="activityDesc">
                    <el-input type="textarea" :rows="3" v-model="formInline4.activityDesc"
                     placeholder="请输入活动说明"></el-input>
                  </el-form-item>
            </el-form>
            
              <el-row>
                <el-col :span="24" style="text-align: center;margin-top: 30px">
                  <el-button 
                    type="info"
                    @click="closeGoodsDialog2('formInline4')" 
                    size="mini"
                  >取 消</el-button>
                  <el-button
                    type="primary"
                    @click="insertGifts2('formInline4')"
                    size="mini"
                  >确 认</el-button>
                </el-col>
              </el-row>
          </div>
        </el-dialog>

    </div>
</template>
<script>
import {
  queryGiftActivityDetail,
  qryGoodsTypeList,
  queryChooseGoodsList,
  queryChooseGiftsList,
  saveGiftActivityWithGoods,
} from "@/api/giftActivity";
import {
  qrySpecialActivityList,
  saveSpecialActivityConfig,
  deleteGoodsActivity,
  qrySpecialActivityInfo
} from "@/api/giftActivity";
const curDateStr = function (){
  var curDate = new Date();
  var year = curDate.getFullYear();
  var month = curDate.getMonth() + 1;
  month = month > 9 ? month : "0" + month;
  var date = curDate.getDate()+1;
  date = date > 9 ? date : "0" + date;
  return year + "-" + month + "-" + date;
}

const curDateStr7 = function (){
  var curDate = new Date();
  var year = curDate.getFullYear();
  var month = curDate.getMonth() + 1;
  month = month > 9 ? month : "0" + month;
  var date = curDate.getDate()+7;
  date = date > 9 ? date : "0" + date;
  return year + "-" + month + "-" + date;
}

export default {
  data() {
    return {
      disabledFlag: false,
      insertGoodsDialogVisible: false,//挑选商品弹窗
      insertGoodsDialogVisible2: false,
      batchSelGifts: [],//记录挑选多选id
      insertGiftsDialogVisible: false,//挑选赠品弹窗
      typeData: [],//商品类型
      ruleForm: {
        uuid: null,
        activityCode: "",
        activityType: "1",
        activityName: "",
        runTimeRange: [],
        remark: "",
        isRelatedGoodsType: "0",
        goodsTypeId: "",
        status: 0,
        goodsCount: "",//商品数量
      },
      rules: {
        activityName: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        runTimeRange: [
          { required: true, message: "请选择活动时间", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "请输入活动说明", trigger: "blur" }
        ],
      },
      rulesGoods: {
        bonuspointMultiple: {required: true, message: '积分倍数不能为空', trigger: 'blur'},
        validDateRange: {type: 'array', required: true, message: '请选择活动时间', trigger: 'change'},
        activityDesc: {required: true, message: '活动说明不能为空', trigger: 'blur'},
      },
      loading: true,
      loading2: true,
      loading3: true,
      submitLoading: false,
      total: 0,
      total2: 0,
      total3: 0,
      listQuery: {
        current: 1,
        size: 100
      },
      listQuery2: {
        current: 1,
        size: 5
      },
      listQuery3: {
        current: 1,
        size: 5
      },
      sort: "",
      order: "",
      goodsList: [],//商品列表
      chooseGoodsList: [],//挑选商品列表
      batchSelGoods: [],//记录挑选多选商品id
      editGoodsListIndex: "",//记录编辑的商品列表序号
      editGoodsId: "",//记录编辑的商品主键
      editActivityGoodsId: "",//记录编辑的活动商品主键
      chooseGiftsList: [],//挑选赠品列表
      activityDesc: [],//记录挑选多选赠品id
      formInline2: {
        goodsName: "",
        goodsSeq: "",
        goodsTypeId: ""
      },//查询2
      formInline3: {
        giftName: "",
        giftCode: "",
        giftType: ""
      },//查询3
      formInline4: {
        goodsIdList:[],
        bonuspointMultiple: "",
        validDateRange: [],
        activityDesc: ""
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "未来3天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 3);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "未来一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "未来一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          }
        ],
        disabledDate(time){
          return time.getTime() < Date.now()-8.64e7   //如果当天可选，就不用减8.64e7
        }
      },
      pickerLimitOptions: {
        disabledDate:time=>{
            let delay = this.ruleForm.runTimeRange;
            if(delay){
              return new Date(delay[1]).getTime() < time.getTime() || time.getTime() < new Date(delay[0]).getTime()-8.64e7;
            }
        }
      },
    };
  },
  mounted() {
    this.disabledFlag = this.$route.params.flag;
    var v = this.$route.params.edit;
    if (v) {
      this.queryGiftActivityDetail(v);
    }else{
      this.loading = false;
    }
  },
  methods: {
    //获取商品分类列表
    getProductType(v) {
      let data = {};
      qryGoodsTypeList(data).then(res => {
        this.typeData = res.result;
      });
    },
    //查询活动详细
    queryGiftActivityDetail(v){
      let data = {uuid: v.uuid};
      // 修改，回显值
      this.ruleForm.uuid = v.uuid;
      // this.ruleForm.activityCode = v.activityCode;
      // this.ruleForm.activityType = v.activityType.toString();
      this.ruleForm.activityName = v.activityName;
      this.ruleForm.runTimeRange = [v.beginDate.toString(), v.endDate.toString()];
      this.ruleForm.remark = v.remark;
      this.ruleForm.status = v.status;
      qrySpecialActivityInfo(data).then(res => {
        if(res.code == 1 && res.result != null){
          this.loading = false;
          let activityDetail = res.result;
          this.goodsList = activityDetail.goodsList;
        }
      });
    },
    //删除商品行
    deleteGoodsItem(row, index){
      this.goodsList.splice(index, 1);
      this.ruleForm.goodsCount = this.goodsList.length  == 0 ? "" : this.goodsList.length;
    },
    
    /******商品弹窗方法********/
    //显示挑选商品弹窗
    showGoodsDialog(){
      this.queryChooseGoodsList();
      this.insertGoodsDialogVisible = true;
    },
    //关闭挑选商品弹窗
    closeGoodsDialog(){
      this.insertGoodsDialogVisible = false;
      this.chooseGoodsList = [];
    },
    //挑选商品列表
    queryChooseGoodsList(){
      let includeGoods = [];
      this.goodsList.forEach((item, index) => {          
        includeGoods.push(item.goodsId);
      });
      let data = {
        current: this.listQuery2.current,
        size: this.listQuery2.size,
        condition: {
          activityId: this.ruleForm.uuid,
          includeGoods: includeGoods.length == 0 ? null : includeGoods,
          goodsName: this.formInline2.goodsName.trim(),
          goodsSeq: this.formInline2.goodsSeq.trim(),
          goodsTypeId: this.formInline2.goodsTypeId
        },
        ascs: [],
        descs: []
      };
      queryChooseGoodsList(data).then(res => {
        this.loading2 = false;
        this.chooseGoodsList = res.records;
        this.total2 = res.total;
      });
    },
    handleSizeChange2(val) {
      this.listQuery2.size = val;
      this.queryChooseGoodsList();
    },
    handleCurrentChange2(val) {
      this.listQuery2.current = val;
      this.queryChooseGoodsList();
    },
    //查询挑选商品
    search2(){
      this.listQuery2.current = 1;
      this.queryChooseGoodsList();
    },
    //重置挑选商品
    kong2(){
      this.formInline2.goodsSeq = "";
      this.formInline2.goodsName = "";
      this.formInline2.goodsTypeId = "";
    },
    //监听选择器
    handleSelectionChange2(val) {
      this.batchSelGoods = val;
    },
    //添加商品
    insertGoods(){
      if(this.batchSelGoods.length == 0){
         this.$message({
            showClose: true,
            message: "请先挑选纳入活动的商品",
            type: "info"
          });
          return false;
      }
      for (let index = 0; index < this.batchSelGoods.length; index++) {
        let row = this.batchSelGoods[index];
        let newGoods = {
          uuid: row.activityGoodsId == "" ? null : row.activityGoodsId,
          activityId: this.ruleForm.uuid,
          goodsId: row.uuid,
          goodsSeq: row.goodsSeq,
          goodsName: row.goodsName,
          activityDesc: "",
          unitName: row.unitName,
          bonuspointMultiple: 1,
          buyCount: 1,
          giftList: [],
          // validDateRange:this.ruleForm.runTimeRange,
          beginDate:this.ruleForm.runTimeRange[0],
          beginDate:this.ruleForm.runTimeRange[1],
        };
        this.goodsList.splice(0, 0, newGoods);
        this.ruleForm.goodsCount = this.goodsList.length  == 0 ? "" : this.goodsList.length;
      }
      this.closeGoodsDialog();
    },
    /******赠品弹窗方法********/
    //显示挑选赠品弹窗
    showGiftsDialog2(row, index){
      this.editGoodsListIndex = index;
      this.editGoodsId= row.goodsId;
      this.editActivityGoodsId = row.activityGoodsId;
      this.insertGiftsDialogVisible = true;
      let $table = this.$refs.goodsTable;
      $table.toggleRowExpansion(row, true);
      this.queryChooseGiftsList();
    },
    //关闭挑选赠品弹窗
    closeGiftsDialog(){
      this.insertGiftsDialogVisible = false;
      this.chooseGiftsList = [];
    },
    //挑选赠品列表
    queryChooseGiftsList(){
      let includeGifts = [];
      this.goodsList[this.editGoodsListIndex].giftList.forEach((item, index) => {          
        includeGifts.push(item.giftId);
      });
      let data = {
        current: this.listQuery3.current,
        size: this.listQuery3.size,
        condition: {
          activityId: this.ruleForm.uuid,
          goodsId: this.editGoodsId,
          status: 1,
          activityType: 1,
          includeGifts: includeGifts.length == 0 ? null : includeGifts,
          giftName: this.formInline3.giftName.trim(),
          giftCode: this.formInline3.giftCode.trim(),
          giftType: this.formInline3.giftType,
        },
        ascs: [],
        descs: []
      };
      queryChooseGiftsList(data).then(res => {
        this.loading3 = false;
        this.chooseGiftsList = res.records;
        this.total3 = res.total;
      });
    },
    handleSizeChange3(val) {
      this.listQuery3.size = val;
      this.queryChooseGiftsList();
    },
    handleCurrentChange3(val) {
      this.listQuery3.current = val;
      this.queryChooseGiftsList();
    },
    //查询挑选赠品
    search3(){
      this.listQuery3.current = 1;
      this.queryChooseGiftsList();
    },
    //重置挑选赠品
    kong3(){
      this.formInline3.giftName = "";
      this.formInline3.giftCode = "";
      this.formInline3.giftType = "";
    },
    //监听选择器2
    handleSelectionChange3(val) {
      this.batchSelGifts = val;
    },
    //添加赠品
    insertGifts(){
      if(this.activityDesc == ""){
         this.$message({
            showClose: true,
            message: "请输入活动说明",
            type: "info"
          });
          return false;
      }
      this.goodsList.find((item)=>{
                if(item.goodsId === this.editGoodsId){//筛选出匹配数据
                    item.activityDesc = this.activityDesc;
                }
            });
      this.closeGiftsDialog();
    },
    //删除赠品行
    deleteGiftsItem(row, index, prow, pindex){
      this.goodsList[pindex].giftList.splice(index, 1);
    },
    //返回
    goback() {
      this.$router.go("-1");
    },
    //取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push("addBonusPointActivity");
    },
    //保存活动
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.goodsList == null || this.goodsList.length == 0){
            this.$message({showClose: true, message: "活动商品不能为空",type: "info"});
            return;
          }
          
          for(let index = 0; index < this.goodsList.length; index++){
              this.goodsList[index].beginDate = this.goodsList[index].validDateRange[0].substring(0, 10);
              this.goodsList[index].endDate = this.goodsList[index].validDateRange[1].substring(0, 10);
          }

          if(this.ruleForm.runTimeRange != null && this.ruleForm.runTimeRange.length == 2){
            this.ruleForm.beginDate = this.ruleForm.runTimeRange[0].substring(0, 10);
            this.ruleForm.endDate = this.ruleForm.runTimeRange[1].substring(0, 10);
          }
          let data = {
            uuid: this.ruleForm.uuid,
            activityType: this.ruleForm.activityType,
            activityName: this.ruleForm.activityName,
            beginDate: this.ruleForm.beginDate,
            endDate: this.ruleForm.endDate,
            remark: this.ruleForm.remark,
            isRelatedGoodsType: this.ruleForm.isRelatedGoodsType,
            goodsTypeId: this.ruleForm.goodsTypeId,
            status: this.ruleForm.status,
            goodsList: this.goodsList
          };
          this.submitLoading = true;
          saveSpecialActivityConfig(data).then(res => {
            if(res.code == 1){
                this.$message({
                showClose: true,
                message: res.message,
                type: "success"
              });
              //this.submitLoading = false;
              this.$router.push("addBonusPointActivity");
            }else{
              this.submitLoading = false;
              this.$message({
                showClose: true,
                message: res.message,
                type: "error"
              });
            }
          });
        } else {
          return false;
        }
      });
      
    },
    //修改是否关联商品类型
    changeIsRelatedGoodsType(){
      if(this.ruleForm.isRelatedGoodsType == 0){
        this.ruleForm.goodsTypeId = null;
      }
    },
    //修改赠品数量选择
    changeIsLimitQuantity(row, index, prow, pindex){
      if(row.isLimitQuantity == 0){//不限
        this.goodsList[pindex].giftList[index].giftQuantity = null;
      }else if(row.giftQuantity === null || row.giftQuantity === undefined || row.giftQuantity === ""){
          this.goodsList[pindex].giftList[index].giftQuantity = 1;
      }
    },
    //修改有效期类型
    changeValidDateType(row, index, prow, pindex){
      if(row.validDateType == 1){//天数
        this.goodsList[pindex].giftList[index].validDateRange = ['',''];
        this.goodsList[pindex].giftList[index].validBeginDate = null;
        this.goodsList[pindex].giftList[index].validEndDate = null
        if(row.dayCount === null || row.dayCount === undefined || row.dayCount === ""){
          this.goodsList[pindex].giftList[index].dayCount = 1;
        }
      }else{//有效期
        this.goodsList[pindex].giftList[index].dayCount = null;
        if(row.validBeginDate === null || row.validBeginDate === undefined || row.validBeginDate === ""){
          this.goodsList[pindex].giftList[index].validBeginDate = curDateStr();
          this.goodsList[pindex].giftList[index].validEndDate = curDateStr();
          this.goodsList[pindex].giftList[index].validDateRange = [curDateStr(),curDateStr()];
        }  
      }
    },
    //修改有效期
    changeValidDate(){
        if(this.ruleForm.runTimeRange != null && this.ruleForm.runTimeRange != undefined && this.ruleForm.runTimeRange.length == 2){
          this.goodsList.forEach((item, index) => {
            item.validDateRange = this.ruleForm.runTimeRange
        });
        }
    },
    //购买数量校验
    buyCountCheck(row, index) {
      let val = row.bonuspointMultiple;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "info"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "info"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "info"});
        val = 1;
      }
      this.goodsList[index].buyCount = parseInt(val);
      return true;
    },
    //礼品总数量
    giftSumQuantityCheck(row, index, prow, pindex) {
      let val = row.giftQuantity;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "info"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "info"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "info"});
        val = 1;
      }
      this.goodsList[pindex].giftList[index].giftQuantity = parseInt(val);
      this.goodsList[pindex].giftList[index].isLimitQuantity = 1;
      return true;
    },
    //天数
    dayCountCheck(row, index, prow, pindex) {
      let val = row.dayCount;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "info"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "info"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "info"});
        val = 1;
      }
      this.goodsList[pindex].giftList[index].dayCount = parseInt(val);
      return true;
    },
    //赠送数量校验
    giveCountCheck(row, index, prow, pindex){
      let val = row.giveCount;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "info"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "info"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "info"});
        val = 1;
      } else {
        if(row.isLimitQuantity == 1 && row.giftQuantity < val){
          this.$message({showClose: true, message: "填入数量不能大于总数量",type: "info"});
          val = 1;
        }
      }
      this.goodsList[pindex].giftList[index].giveCount = parseInt(val);
      return true;
    },
    fixData(row, column, cellValue, index) {
      if (cellValue != null && !isNaN(cellValue)) {
        return cellValue.toFixed(2);
      } else {
        return cellValue;
      }
    },
    showGiftsDialog(row, index){
      this.editGoodsListIndex = index;
      this.editGoodsId = row.goodsId;
      this.activityDesc = row.activityDesc;
      this.editActivityGoodsId = row.activityGoodsId;
      this.insertGiftsDialogVisible = true;
    },
    showGoodsDialog2(){
      if(this.batchSelGifts.length == 0){
         this.$message({
            showClose: true,
            message: "请先挑选商品",
            type: "info"
          });
          return false;
      }
      this.insertGoodsDialogVisible2 = true;
      this.formInline4.bonuspointMultiple = "";
      this.formInline4.validDateRange = this.ruleForm.runTimeRange;
      this.formInline4.activityDesc = "";
    },
    closeGoodsDialog2(formName){
      this.$refs[formName].resetFields();
      this.insertGoodsDialogVisible2 = false;
    },
    insertGifts2(formName){
      this.$refs[formName].validate((valid) => {
      if(valid){
      for (let index = 0; index < this.batchSelGifts.length; index++) {
        let row = this.batchSelGifts[index];
        this.goodsList.find((item)=>{
                if(item.goodsId === row.goodsId){//筛选出匹配数据
                    item.bonuspointMultiple = this.formInline4.bonuspointMultiple;
                    item.validDateRange = this.formInline4.validDateRange;
                    item.activityDesc = this.formInline4.activityDesc;
                }
            });
      }
      this.closeGoodsDialog2('formInline4');
      }
      })
    },

  },//其他
  computed: {
    isGiftEmpty: function () {
      if(this.goodsList != null && this.goodsList.length > 0){
        for(let i = 0; i < this.goodsList.length; i++){
          if(this.goodsList[i].giftList != null && this.goodsList[i].giftList.length > 0){
            for(let j = 0; j < this.goodsList[i].giftList.length; j++){
              let gift = this.goodsList[i].giftList[j];
              if(gift.isLimitQuantity == 0){//不限
                return "";
              }else{
                if(gift.giftQuantity == null || gift.giftQuantity > 0){
                  return "";
                }
              }
            }
          }else{
            return "";
          }
        }
        return "（赠品已赠完）";
      }else{
        return "";
      }
    },
    
  }
};
</script>
<style scoped>
#giftActivity /deep/ .giftTable table th, #giftActivity .giftTable table td{
  border:1px solid #EBEEF5;
}
#giftActivity /deep/ .giftTable table th{
  background: #eef1f6;
  color:#606266
}
#giftActivity /deep/ .giftTable table .el-radio-button-inner-child .el-radio-button__inner{
  padding:0;
}
#giftActivity /deep/ .giftTable table .el-radio-button-inner-child .el-input__inner{
  border:none;
  height:30px;
}
#giftActivity /deep/ .el-dialog__body{
  padding-top:0px;
}
#giftActivityGoodsTable /deep/ .el-input__inner[type='number']{
  padding:0;
}
</style>
